package com.karumi.dexter;

import com.karumi.dexter.uitool.DexterLogUtil;

import ohos.aafwk.ability.Ability;
import ohos.aafwk.content.Intent;
import ohos.agp.window.service.WindowManager;
import ohos.bundle.IBundleManager;

import java.util.Collection;
import java.util.LinkedList;

/**
 * 权限申请ability，类似提供了一个平台
 * 因为主题问题，暂时不启用
 */
@Deprecated
public class DexterAbility extends Ability {
    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
//        setUIContent(ResourceTable.Layout_layout_test_show);
        DexterLogUtil.info("dexterLogUtil", "18-1");
        Dexter.onAbilityReady(this);
        getWindow().addFlags(WindowManager.LayoutConfig.MARK_TOUCH_MODAL_IMPOSSIBLE);
    }

    @Override
    protected void onStop() {
        super.onStop();
        DexterLogUtil.info("dexterLogUtil", "18-2");
        Dexter.onAbilityDestroyed(this);
    }

    @Override
    public void onRequestPermissionsFromUserResult(int requestCode, String[] permissions, int[] grantResults) {
        super.onRequestPermissionsFromUserResult(requestCode, permissions, grantResults);
        DexterLogUtil.info("dexterLogUtil", "18-3-onRequestPermissionsFromUserResult");
        Collection<String> grantedPermissions = new LinkedList<>();
        Collection<String> deniedPermissions = new LinkedList<>();

        for (int i = 0; i < permissions.length; i++) {
            String permission = permissions[i];
            switch (grantResults[i]) {
                case IBundleManager.PERMISSION_DENIED:
                    deniedPermissions.add(permission);
                    break;
                case IBundleManager.PERMISSION_GRANTED:
                    grantedPermissions.add(permission);
                    break;
                default:
            }
        }
        DexterLogUtil.info(
                "dexterLogUtil",
                "18-3-1:grantedPermissions"
                        + grantedPermissions.toString()
                        + "-deniedPermissions:"
                        + deniedPermissions.toString());
        Dexter.onPermissionsRequested(grantedPermissions, deniedPermissions);
    }
}
